package vn.appsmobi.ui;


import vn.appsmobi.R;
import vn.appsmobi.activity.AppCommentsActivity;
import vn.appsmobi.adapter.AppCommentsAdapter;
import vn.appsmobi.loader.AppCommentsLoader;
import vn.appsmobi.loader.AppCommentsLoader.Result;
import vn.appsmobi.loader.BaseLoader;
import vn.appsmobi.loader.Pager;
import vn.appsmobi.model.AppInfo;
import vn.appsmobi.ulti.Log;
import vn.appsmobi.widget.EmptyLoadingView;
import vn.appsmobi.widget.Refreshable;
import android.os.Bundle;
import android.support.v4.app.LoaderManager;
import android.support.v4.content.Loader;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.ListView;

public class AppCommentsFragment extends BaseFragment implements LoaderManager.LoaderCallbacks<AppCommentsLoader.Result>, Refreshable{
	
  static final int APP_COMMENT_LOADER = 0;		
  private AppCommentsActivity mActivity;
  protected AppCommentsAdapter mAdapter;
  private String mAppId;
  private View mFooterView;
  protected boolean mIsReachedBottom;
  protected ListView mListView;
  private AppCommentsLoader mLoader;
  protected EmptyLoadingView mLoadingView;
  private AbsListView.OnScrollListener mOnScrollListener = new Pager(new Runnable()
  {
    public void run()
    {
      if ((AppCommentsFragment.this.mLoader != null) && (!AppCommentsFragment.this.mLoader.isLoading()) && (!AppCommentsFragment.this.mIsReachedBottom))
      {
    	
        AppCommentsFragment.this.mLoader.nextPage();
        AppCommentsFragment.this.mLoader.forceLoad();
      }
    }
  });
  protected View mRootView;

  /**Phong
 * 
 */
public AppCommentsFragment() {
	
}

public void onActivityCreated(Bundle bundle)
  {
	Log.v("app comment fragment create");
    super.onActivityCreated(bundle);
    this.mActivity = ((AppCommentsActivity)getActivity());
    this.mLoadingView.setRefreshable(this);
    this.mLoadingView.setEmptyText(getString(R.string.app_comments_empty));
   // this.mLoadingView.setDefaultSuccessText(getString(R.string.app_comments_empty));
    this.mLoadingView.setNoNewDataCallback(new EmptyLoadingView.NoNewDataCallback()
    {
      public boolean onNoNewData()
      {
        AppCommentsFragment.this.mIsReachedBottom = true;
        return false;
      }
    });
    this.mListView.setEmptyView(this.mLoadingView);
    this.mAdapter = new AppCommentsAdapter(this.mActivity);
    AppInfo appinfo = AppInfo.get(this.mAppId);
    this.mAdapter.setCurrentVersion(appinfo.getVersion());
    this.mListView.setAdapter(this.mAdapter);
    this.mListView.setOnScrollListener(this.mOnScrollListener);
    getLoaderManager().initLoader(0, null, this);
  }

  public Loader  onCreateLoader(int id, Bundle bundle)
  {
	  Log.v("App Comment  create loader");
    if (id == APP_COMMENT_LOADER)
    {
      this.mLoader = new AppCommentsLoader(this.mActivity);
      this.mLoader.setProgressNotifiable(this.mLoadingView);
      //this.mLoader.setCommentType(this.mCommentType);
      this.mLoader.setAppId(this.mAppId);
    }
    /*for (AppCommentsLoader localAppCommentsLoader = this.mLoader; ; localAppCommentsLoader = null)
      return localAppCommentsLoader;
      */
    BaseLoader baseloader = this.mLoader;
	return baseloader;
  }

  public View onCreateView(LayoutInflater inflate, ViewGroup parent, Bundle bundle)
  {
    this.mRootView = inflate.inflate(R.layout.common_list_view, null);
    this.mListView = ((ListView)this.mRootView.findViewById(R.id.list));
    this.mLoadingView = ((EmptyLoadingView)this.mRootView.findViewById(R.id.loading));
    this.mFooterView = inflate.inflate(R.layout.app_comments_list_footer, null);
    this.mListView.addFooterView(this.mFooterView, null, false);
    return this.mRootView;
  }

  public void onDestroyView()
  {
    this.mListView.removeFooterView(this.mFooterView);
    super.onDestroyView();
  }

  public void onLoadFinished(Loader<AppCommentsLoader.Result> paramLoader, AppCommentsLoader.Result result)
  {
	if(mLoader.mPage == 1 ||(result.mAppCommentsList != null && !result.mAppCommentsList.isEmpty()) )
	{
		this.mActivity.updateCommentSummary(result);   
		this.mAdapter.updateData(result.mAppCommentsList);	   
	}
	 
  }

  

  public void refreshData()
  {
    this.mIsReachedBottom = false;
    if (this.mLoader != null)
    {
      this.mLoader.reload();
      this.mListView.setSelection(0);
    }
  }

  public void setAppId(String paramString)
  {
    this.mAppId = paramString;
  }

  public void setType(int paramInt)
  {
  }

	@Override
	public void onLoaderReset(Loader<Result> arg0) {
		// TODO Auto-generated method stub
		
	}

}
